<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Upload_svg extends CI_Controller {

    public function __construct(){
        parent::__construct();   
        $this->form_validation->set_error_delimiters('<div class="error_field" ><span class="fleche"></span><div>', '</div></div><br/>');
    }

    public function index() {
      $this->load->view('step_1');
    }
    
    public function step_one() {

        $config['upload_path']      = RESOURCES_PATH;
        $config['allowed_types']    = 'zip|rar';
        $config['overwrite']        = true;
        $this->form_validation->set_rules('project_name', 'Le nom du projet', 'required|xss_clean');
        if ( $this->form_validation->run() == FALSE)
        {
            $this->load->view('step_1');
            return;
        }
        $project_name = $this->input->post('project_name');
        $this->load->library('upload', $config);
        $file_name = "project_res";

        if (!$this->upload->do_upload($file_name)) {
            
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('step_one', $error);
        } else {
            $data = array(
                "project_name" => $project_name,
                "resource"     => $this->upload->data()
            );
            $this->step_two($data);
        }
    }
    
    public function step_two($data = null) {

        if ($data == null || !is_array($data))
            return;
        $this->load->helper('file');
        $path = PROJECTS_PATH . $data['project_name'];
        if(is_dir($path)) 
        {
            
           $info['error'] = "Le projet :<u>". $data['project_name'] ." existe deja !";
        } 
        else 
        {

            if (!create_dir($path)) 
            {
               $info['error'] ="Des erreur sont survenu pandont la creation du repertoire :$path";
            } 
            else 
            {
                $this->load->helper('directory');
                $source = RESOURCES_PATH .  $data['resource']['file_name'];
                if( extract_zip_to($source, $path))
                {
                    $info['svg_files'] = directory_map($path , 1);
                    $info['file_tree'] = create_tree( $info['svg_files']);
                    $info['list_item'] = get_list_items($info['file_tree']);
                    $info['list'] = create_navigation_menu( $info['file_tree'] );
                   
                }
                else
                {
                 $info['error'] = "le fichier". $data['resource']['file_name']." n'a pas pus être extrait "; 
                }
            }
        }
        
        $this->load->view('step_2' , $info);
    }

    public function ziper(){
        $this->load->library('zip');
        
        /* ARchiver un fichier */
       // $file_to_add = RESOURCES_PATH . '/0 Vue Processus.svg';
        //$this->zip->read_file($file_to_add);
        
        /* Archiver un dosser avec son contenu */
        $this->zip->read_dir(RESOURCES_PATH.'folder_to_zip/' , FALSE); 

        // Write the zip file to a folder on your server. Name it "my_backup.zip" or path/to/my_backup.zip
        $this->zip->archive(RESOURCES_PATH . 'my_backup.zip');
        
        // Download the file to your desktop. Name it "my_backup.zip"
       // $this->zip->download('_my_backup.zip');
        //$this->load->view('test_view');
    }
    
    public function extract(){
       
        extract_zip_to(RESOURCES_PATH.'export.rar', RESOURCES_PATH.'/test');
    }
    
    public function read_directory(){
        $this->load->helper('directory');
        $map = directory_map(RESOURCES_PATH, 1);
        echo json_encode($map);
    }
    public function read_file(){
        $this->load->helper('file');
       $s = read_file(RESOURCES_PATH.'/tst.svg');
       if($s == false)
           echo 'con not read the file ';
       else
       echo $s;
    }
    public function write_file(){
        $this->load->helper('file');
        $data = $this->load->view('test_view' ,'', true);
        if (!write_file(RESOURCES_PATH.'/test.html', $data)) {
            echo 'Unable to write the file';
        } else {
            echo 'File written!';
        }
    }
    
    

}

























/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */